﻿using System;
using System.Diagnostics;
using System.IO;
using System.ServiceModel;

namespace jbt.CodeCandy.AsTrEAM.Contracts
{
    /// <summary>
    /// Data related to a stream.
    /// </summary>
    [MessageContract(WrapperNamespace = Settings.WCF.Namespage)]
    [DebuggerDisplay("Id: {Id}; Name: {Name}; LastModified: {LastModified};")]
    public class StreamData : IDisposable
    {
        /// <summary>
        /// Gets or sets the content of stream.
        /// </summary>
        [MessageBodyMember(Namespace = Settings.WCF.Namespage)]
        public Stream Content
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the name of encoding of the content.
        /// </summary>
        [MessageHeader(Namespace = Settings.WCF.Namespage)]
        public string ContentEncoding
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the length of content.
        /// </summary>
        [MessageHeader(Namespace = Settings.WCF.Namespage)]
        public long ContentLength
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the HTTP MIME type of content.
        /// </summary>
        [MessageHeader(Namespace = Settings.WCF.Namespage)]
        public string ContentType
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the unique identifier of stream.
        /// </summary>
        [MessageHeader(Namespace = Settings.WCF.Namespage)]
        public string Id
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the date stream was last modified.
        /// </summary>
        [MessageHeader(Namespace = Settings.WCF.Namespage)]
        public DateTime LastModified
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the name of the stream.
        /// I.e. for file streams this is the name of file.
        /// </summary>
        [MessageHeader(Namespace = Settings.WCF.Namespage)]
        public string Name
        {
            get;
            set;
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Content != null)
                {
                    Content.Dispose();
                }
            }
        }

        #region IDisposable Members

        /// <summary>
        /// Releases all resources used by this instance.
        /// </summary>
        public void Dispose()
        {
            const bool disposing = true;
            Dispose(disposing);

            GC.SuppressFinalize(this);
        }

        #endregion
    }
}